import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import VueSetupExtend from "vite-plugin-vue-setup-extend";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import svgLoader from "vite-svg-loader";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";
import { ArcoResolver } from 'unplugin-vue-components/resolvers';
import { vitePluginForArco } from '@arco-plugins/vite-vue';
import { viteCommonjs } from '@originjs/vite-plugin-commonjs'
import Unocss from 'unocss/vite'
import path from "path";
// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    VueSetupExtend(),
    svgLoader(),
    createSvgIconsPlugin({
      iconDirs: [path.resolve(process.cwd(), "src/assets/icons/svg")],
      symbolId: "icon-[dir]-[name]",
      inject: "body-last",
      customDomId: "__svg__icons__dom__",
    }),
    AutoImport({
      imports: ["vue", "vue-router", "pinia"],
      dts: "src/types/auto-imports.d.ts",
      dirs: [
        "./src/composables/**",
        "./src/services/**",
        "./src/store/**",
      ],
      vueTemplate: true,
      resolvers: [
        ArcoResolver(),
      ],
    }),
    Components({
      dts: "src/types/components.d.ts", // 自动生成.d.ts文
      dirs: [
        'src/components',      // 公共组件
      ],
      resolvers: [
        // // 自定义组件命名规则
        // (name) => {
        //   if (name.startsWith('Base'))
        //     return { importName: name, path: 'common-components' }
        // }
        ArcoResolver({
          sideEffect: true
        }),
      ],
      types: [{
        from: 'vue-router',
        names: ['RouterLink', 'RouterView']
      }]
    }),
    vitePluginForArco({
      style: 'css'
    }),
    Unocss(),
    viteCommonjs()
  ],
  optimizeDeps: {
    include: [
      '@arco-design/web-vue/es/locale/lang/zh-cn',
      '@vueuse/core',
    ],
  },
  resolve: {
    alias: {
      "@": path.resolve(__dirname, './src'),
    },
  },
  server: {
    port: 3000,
    open: true,
    proxy: {
      '/api': {
        target: 'http://localhost:8080',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '')
      }
    }
  },
  build: {
    outDir: 'dist',
    assetsDir: 'assets',
    rollupOptions: {
      input: {
        main: path.resolve(__dirname, 'index.html'),
        // other html files
      },
    },
  }
})
